package com.qk.management.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.businesses.BusinessDTO;
import com.qk.dto.businesses.BusinessQueryDTO;
import com.qk.dto.businesses.BusinessTrackRecordDTO;
import com.qk.dto.businesses.UpdateBusinessDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import com.qk.vo.business.GetBusinessTrackRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author kaoM1Jy
 * @date 2025/10/15 11:04
 * @description:
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;


    @GetMapping("/pool")
    public Result getBusinessPool(BusinessTrackRecordDTO dto) {
        PageResult<GetBusinessTrackRecordVO> result = businessService.getBusinessPool(dto);
        return Result.success(result);
    }




    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessDTO dto) {
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        BusinessVO vo =  businessService.getBusinessById(id);
        return Result.success(vo);
    }


    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
           businessService.toCustomer(id);
        return Result.success();
    }




    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        businessService.update(Wrappers.lambdaUpdate(Business.class)
                .eq(Business::getId, id)
                .set(Business::getStatus, BusinessStatusConstants.RECYCLE)
                .set(Business::getUpdateTime, LocalDateTime.now())
                .set(Business::getUserId, null)
        );

        return Result.success();
    }


    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }


    @GetMapping
    public Result getBusinessList(BusinessQueryDTO dto) {
        PageResult<BusinessDO> result = businessService.getBusinessList(dto);
        return Result.success(result);
    }


    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto) {
        businessService.addBusiness(dto);
        return Result.success();
    }
}

